/*
 * Copyright (c) 2015 Tarun Gupta
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include <Swiften/Serializer/PayloadSerializers/IsodeIQDelegationSerializer.h>
#include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h>
#include <Swiften/Elements/IsodeIQDelegation.h>
#include <Swiften/Elements/Delay.h>
#include <Swiften/Elements/Forwarded.h>
#include <Swiften/Elements/IQ.h>
#include <Swiften/Elements/Message.h>
#include <Swiften/Elements/Presence.h>
#include <Swiften/Elements/Subject.h>
#include <Swiften/JID/JID.h>
#include <Swiften/Base/DateTime.h>

using namespace Swift;

class IsodeIQDelegationSerializerTest : public CppUnit::TestFixture
{
		CPPUNIT_TEST_SUITE(IsodeIQDelegationSerializerTest);
		CPPUNIT_TEST(testSerialize_Forwarded_IQ);
		CPPUNIT_TEST(testSerialize_Forwarded_Message);
		CPPUNIT_TEST(testSerialize_Forwarded_MessageNoDelay);
		CPPUNIT_TEST(testSerialize_Forwarded_Presence);
		CPPUNIT_TEST_SUITE_END();

	public:
		IsodeIQDelegationSerializerTest() {}

		void testSerialize_Forwarded_IQ() {
			IsodeIQDelegationSerializer testling(&serializers);
			boost::shared_ptr<IsodeIQDelegation> isodeIQDelegation = boost::make_shared<IsodeIQDelegation>();

			boost::shared_ptr<IQ> iq = IQ::createResult(JID("juliet@capulet.lit/balcony"), JID("romeo@montague.lit/orchard"), "id0", boost::make_shared<Subject>("text"));

			boost::shared_ptr<Forwarded> forwarded(boost::make_shared<Forwarded>());
			forwarded->setStanza(iq);
			forwarded->setDelay(boost::make_shared<Delay>(stringToDateTime(std::string("2010-07-10T23:08:25Z"))));
			isodeIQDelegation->setForward(forwarded);

			CPPUNIT_ASSERT_EQUAL(std::string("<delegate xmlns=\"http://isode.com/iq_delegation\">"
								"<forwarded xmlns=\"urn:xmpp:forward:0\">"
								"<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>"
								"<iq from=\"romeo@montague.lit/orchard\" id=\"id0\" to=\"juliet@capulet.lit/balcony\" type=\"result\" xmlns=\"jabber:client\"><subject>text</subject></iq>"
								"</forwarded>"
								"</delegate>"), testling.serialize(isodeIQDelegation));	
		}

		void testSerialize_Forwarded_Message() {
			IsodeIQDelegationSerializer testling(&serializers);
			boost::shared_ptr<IsodeIQDelegation> isodeIQDelegation = boost::make_shared<IsodeIQDelegation>();

			boost::shared_ptr<Message> message(boost::make_shared<Message>());
			message->setType(Message::Chat);
			message->setTo(JID("juliet@capulet.lit/balcony"));
			message->setFrom(JID("romeo@montague.lit/orchard"));
			message->setBody("Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.");

			boost::shared_ptr<Forwarded> forwarded(boost::make_shared<Forwarded>());
			forwarded->setStanza(message);
			forwarded->setDelay(boost::make_shared<Delay>(stringToDateTime(std::string("2010-07-10T23:08:25Z"))));

			isodeIQDelegation->setForward(forwarded);
			CPPUNIT_ASSERT_EQUAL(std::string("<delegate xmlns=\"http://isode.com/iq_delegation\">"
		  										"<forwarded xmlns=\"urn:xmpp:forward:0\">"
										        "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>"
										        "<message from=\"romeo@montague.lit/orchard\" to=\"juliet@capulet.lit/balcony\" type=\"chat\" xmlns=\"jabber:client\">"
										            "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>"
										        "</message>"
										    	"</forwarded>"
		  									"</delegate>"), testling.serialize(isodeIQDelegation));	
		}

		void testSerialize_Forwarded_MessageNoDelay() {
			IsodeIQDelegationSerializer testling(&serializers);
			boost::shared_ptr<IsodeIQDelegation> isodeIQDelegation = boost::make_shared<IsodeIQDelegation>();

			boost::shared_ptr<Message> message(boost::make_shared<Message>());
			message->setType(Message::Chat);
			message->setTo(JID("juliet@capulet.lit/balcony"));
			message->setFrom(JID("romeo@montague.lit/orchard"));
			message->setBody("Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.");

			boost::shared_ptr<Forwarded> forwarded(boost::make_shared<Forwarded>());
			forwarded->setStanza(message);
			isodeIQDelegation->setForward(forwarded);

			CPPUNIT_ASSERT_EQUAL(std::string("<delegate xmlns=\"http://isode.com/iq_delegation\">"
											   	"<forwarded xmlns=\"urn:xmpp:forward:0\">"
											        "<message from=\"romeo@montague.lit/orchard\" to=\"juliet@capulet.lit/balcony\" type=\"chat\" xmlns=\"jabber:client\">"
											            "<body>Call me but love, and I'll be new baptized; Henceforth I never will be Romeo.</body>"
											        "</message>"
												"</forwarded>"
		  									"</delegate>"), testling.serialize(isodeIQDelegation));	
		}

		void testSerialize_Forwarded_Presence() {
			IsodeIQDelegationSerializer testling(&serializers);
			boost::shared_ptr<IsodeIQDelegation> isodeIQDelegation = boost::make_shared<IsodeIQDelegation>();

			boost::shared_ptr<Presence> presence(boost::make_shared<Presence>());
			presence->setType(Presence::Subscribe);

			boost::shared_ptr<Forwarded> forwarded(boost::make_shared<Forwarded>());
			forwarded->setStanza(presence);
			forwarded->setDelay(boost::make_shared<Delay>(stringToDateTime(std::string("2010-07-10T23:08:25Z"))));
			isodeIQDelegation->setForward(forwarded);

			CPPUNIT_ASSERT_EQUAL(std::string("<delegate xmlns=\"http://isode.com/iq_delegation\">"
											   	"<forwarded xmlns=\"urn:xmpp:forward:0\">"
											        "<delay stamp=\"2010-07-10T23:08:25Z\" xmlns=\"urn:xmpp:delay\"/>"
											        "<presence type=\"subscribe\" xmlns=\"jabber:client\"/>"
											    "</forwarded>"
											"</delegate>"), testling.serialize(isodeIQDelegation));	
		}
	private:
		FullPayloadSerializerCollection serializers;		
};

CPPUNIT_TEST_SUITE_REGISTRATION(IsodeIQDelegationSerializerTest);
